var orgConservationSelect = "";
var installType = -1;
var signType = -1;
var signFixed = -1;
//按维护单位查询的下拉框
$.getConservationOrg = function (mainOrgId) {
    $.ajax({
        url: '../rds_count/getConservationOrg',
        type: 'post',
        data: {
            mainOrgId: mainOrgId,
            userId: gUserLoginResult.userId,
            orgId: gUserLoginResult.orgId
        },
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    if (msg != null && typeof (msg) != "undefined") {
                        if (msg.isSuccess == true) {
                            orgConservationSelect = '';
                            if (msg.jsonData.length > 1) {
                                orgConservationSelect = "<option value='-1'>养护公司</option>";
                            }
                            if (msg.jsonData.length == 0) {
                                orgConservationSelect = "<option value='-1'>无权限</option>";
                            }
                            for (var i = 0; i < msg.jsonData.length; i++) {
                                orgConservationSelect += "<option value='" + msg.jsonData[i].orgId + "'>" + msg.jsonData[i].orgName + "</option>";
                            }
                            orgConservationSelect += "</select>";
                            $("#orgConservationChoose").html(orgConservationSelect);
                            if (gUserLoginResult.orgType == 0 && msg.jsonData.length == 1) {
                                $("#orgConservationChoose").hide();
                            }
                        }
                    }
                }
            }
        }
    });
};
$(function () {
    gMainDT = $("#mainDataTables").DataTable({
        ajax: {
            url: "../rds_count/getRoadSign4DT",
            type: "POST",
            data: function (d) {
                d.userId = gUserLoginResult.userId;
                d.orgId = gUserLoginResult.orgId;
                d.search = $('#mainDataTables').DataTable().search(this.value);
            }
        },
        columns: [{
            data: "roadSignId"
        }, {
            data: "roadSignPic",
            render: function (data, type, row) {
                var img = "../../images/default_photo.png";
                if (data != null) {
                    if (data.length != 0) {
                        img = data;
                    }
                }
                return "<img src='" + img + "' style='width:50px;height:40px;cursor: pointer' " +
                    "onclick='$.showPicModal(\"" + img + "\")'> ";
            }
        }, {
            data: "roadSignDirection",
            render: function (data, type, row) {
                var directon;
                if (data == 0) {
                    directon = "东";
                }
                else if (data == 1) {
                    directon = "南";
                }
                else if (data == 2) {
                    directon = "西";
                }
                else {
                    directon = "北";
                }
                return directon;
            }
        }, {
            data: "roadSignType",
            render: function (data, type, row) {
                var signType;
                //0:警告标志; 1:禁令标志; 2:指示标志; 3:指路标志; 4:旅游区标志; 5:作业区标志; 6:告示标志; 7:辅助标志
                if (data == 0) {
                    signType = "警告标志";
                }
                else if (data == 1) {
                    signType = "禁令标志";
                }
                else if (data == 2) {
                    signType = "指示标志";
                }
                else if (data == 3) {
                    signType = "指路标志";
                }
                else if (data == 4) {
                    signType = "旅游区标志";
                }
                else if (data == 5) {
                    signType = "作业区标志";
                }
                else if (data == 6) {
                    signType = "告示标志";
                }
                else {
                    signType = "辅助标志";
                }
                return "<button class='btn btn-default btn-xs btn-flat'>" + signType + "</button>";
            }
        }, {
            data: "roadSignInstallType",
            render: function (data, type, row) {
                var installType;
                // 安装方式(0:单标志; 1:合杆;2:借杆)
                if (data == 0) {
                    installType = "单标志";
                }
                else if (data == 1) {
                    installType = "合杆";
                }
                else {
                    installType = "借杆";
                }
                return "<button class='btn btn-default btn-xs btn-flat'>" + installType + "</button>";
            }
        }, {
            data: "roadSignFixed",
            render: function (data, type, row) {
                var roadSignFixed = "";
                // 板面固定(0:扎带; 1:抱箍)
                if (data == 0) {
                    roadSignFixed = "扎带";
                }
                else if (data == 1) {
                    roadSignFixed = "抱箍";
                }
                return "<button class='btn btn-default btn-xs btn-flat'>" + roadSignFixed + "</button>";
            }
        }, {
            data: "roadSignBug",
            render: function (data, type, row) {
                //存在问题（0:正常，1:膜脱落，2:膜淡化，3:逆反射系数不达标，4:卷边，5:转向，6:非国际，7:抱箍缺少，8:净空高不达标，
                // 9:设置不合理，10:缺失，11:建议增设，12:与交通组织矛盾，13:内容有误，14:标牌与标杆不配套，15:非公指示牌）
                var roadSignBug;
                if (data == 0) {
                    roadSignBug = "正常";
                }
                else if (data == 1) {
                    roadSignBug = "膜脱落";
                }
                else if (data == 2) {
                    roadSignBug = "膜淡化";
                }
                else if (data == 3) {
                    roadSignBug = "逆反射系数不达标";
                }
                else if (data == 4) {
                    roadSignBug = "卷边";
                } else if (data == 5) {
                    roadSignBug = "转向";
                } else if (data == 6) {
                    roadSignBug = "非国际";
                } else if (data == 7) {
                    roadSignBug = "抱箍缺少";
                }
                else if (data == 8) {
                    roadSignBug = "净空高不达标";
                }
                else if (data == 9) {
                    roadSignBug = "设置不合理";
                }
                else if (data == 10) {
                    roadSignBug = "缺失";
                }
                else if (data == 11) {
                    roadSignBug = "建议增设";
                }
                else if (data == 12) {
                    roadSignBug = "与交通组织矛盾";
                }
                else if (data == 13) {
                    roadSignBug = "内容有误";
                }
                else if (data == 14) {
                    roadSignBug = "标牌与标杆不配套";
                }
                else {
                    roadSignBug = "非公指示牌";
                }
                return "<button class='btn btn-default btn-xs btn-flat'>" + roadSignBug + "</button>";
            }
        }, {
            data: "roadSignSecurity",
            render: function (data, type, row) {
                //安全状况（0：安全；1：不安全）
                if (data == 0) {
                    return "<button class='btn btn-success btn-xs btn-flat'>安全 </button>";
                }
                else
                    return "<button class='btn btn-danger btn-xs btn-flat'>不安全</button>";
            }
        }, {
            data: "roadSignInstallTime"
        }, {
            data: "orgName"
        }],
        dom: "<'row'<'col-md-2'l><'#mainToolbar.col-md-10 '>r>t<'row'<'col-md-6'i><'col-md-6'p>>",
        initComplete: function () {
            // 下拉工具栏按钮的权限
            $("#mainToolbar").empty();
            var timeLimit = "<div class='input-group input-group-sm'><input type='text' class='form-control' style='width: 175px;' name='timeLimit' id='timeLimit' placeholder='安装时间区间' readonly='readonly'></div>";
            var selectDown = "<div class='input-group input-group-sm'><select class='form-control' id='orgConservationChoose'></select>";
            var strToolBar = "<div class='pull-right'><div class='btn-group'>" +
                "<button type='button' class='btn btn-primary btn-sm btn-flat'><i class='fa fa-cog'></i> 操作</button>";
            strToolBar += "<button type='button' class='btn btn-primary btn-sm btn-flat dropdown-toggle' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false' onclick='$.dateReturn()'>";
            strToolBar += "<span class='caret'></span><span class='sr-only'>切换下拉</span></button><ul class='dropdown-menu'>";
            strToolBar += "<li><button href='#' class='btn btn-primary btn-sm btn-flat btn-block' onclick='gMainDT.ajax.reload(null, false);'><i class='fa fa-refresh'></i> 刷新</button></li>";
            strToolBar += "</ul></div> <button type='button' class='btn btn-primary btn-sm btn-flat' style='margin-right: 5px' onclick='$.toRoadSignCount();'><i class='glyphicon glyphicon-equalizer'></i>标牌数量统计 " +
                "</button><button type='button' class='btn btn-primary btn-sm btn-flat' style='margin-right: 5px' onclick='$.toBarChart();'><i class='fa fa-bar-chart'></i>标志分类数量统计 </button>" + timeLimit + "&nbsp;" + selectDown + "";
            strToolBar += "<span class='input-group-btn'><button class='btn btn-primary btn-flat' type='button' onclick='$(this).searchDT()'><i class='fa fa-search'></i></button></span></div>" +
                " <div class='input-group input-group-sm'><span class='input-group-btn'><button class='btn btn-primary btn-flat' type='button' onclick='$.showSearchModal()'><i class='glyphicon glyphicon-plus'></i></button></span></div></div>";
            $("#mainToolbar").append(strToolBar);
            //让时间插件正常使用
            $("#timeLimit").focus(function () {
                $('#timeLimit').daterangepicker({
                    autoUpdateInput: false,
                    locale: {
                        format: 'YYYY-MM-DD',
                        applyLabel: '确认',
                        cancelLabel: '取消',
                        fromLabel: '从',
                        toLabel: '到',
                        weekLabel: 'W',
                        customRangeLabel: 'Custom Range',
                        daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
                        monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
                    }
                }).on('cancel.daterangepicker', function (ev, picker) {
                    $(this).val('');
                }).on('apply.daterangepicker', function (ev, picker) {
                    $(this).val(picker.startDate.format('YYYY-MM-DD') + ' - ' + picker.endDate.format('YYYY-MM-DD'));
                });
                $(".dropdown-menu").css("min-width", "0");
            });
            $.getConservationOrg(-1);
        },
        processing: true,
        serverSide: true,
        ordering: false,
        select: false,
        autoFill: true,
        displayLength: 20,
        lengthMenu: [[5, 10, 15, 20, 50, 100, 150, -1], [5, 10, 15, 20, 50, 100, 150, "All"]],
        language: {
            url: "zh_cn.json"
        },
        fnDrawCallback: function () {// 序号自增回调函数
            var api = this.api();
            var startIndex = api.context[0]._iDisplayStart;
            api.column(0).nodes().each(function (cell, i) {
                cell.innerHTML = startIndex + i + 1;
            });
        }
    });

});

// 搜索框查询操作
$.fn.searchDT = function () {
    //在对表格进行查询时，如果没有选择值项进行查询，该变量的值就赋值为-1，在后台用特殊字符"#"进行拆分
    var timeLimit = $("input[name='timeLimit']").val();
    if (timeLimit == "") {
        timeLimit = -1;
    }
    var orgName = $("#orgConservationChoose").val();
    var searchContext = timeLimit + "#" + installType + "#" + signFixed + "#" + signType + "#" + orgName;
    gMainDT.search(searchContext).draw();
};

//使时间插件使用正常
$.dateReturn = function () {
    $(".dropdown-menu").css("min-width", "100%");
};

//显示搜索模态框
$.showSearchModal = function () {
    $("#label-search").text("精细查询");
    $("#modal-search").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    });
};

//跳转柱状图
$.toBarChart = function () {
    $("#workbench").load("count/roadSignCountBar.html");
};

$.toRoadSignCount = function () {
    $("#workbench").load("count/roadSignCountSumBar.html");
};

//重置搜索条件
$.reStart = function () {
    $("#selectInstallType").val("-1");
    $("#selectSignType").val("-1");
    $("#selectSignFixed").val("-1");
};

//精细搜索
$.mySearch = function () {
    installType = $("#selectInstallType").val();
    signType = $("#selectSignType").val();
    signFixed = $("#selectSignFixed").val();
    $.fn.searchDT();
};